//
//  LoginPhoneViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 12.04.25.
//

import Foundation
class LoginPhoneViewModel : ObservableObject {
    
    @Published var isLoading = false
    @Published var loginState = ""
    @Published var captchaState: String = ""
    
    func phoneLogin(phone: String, captcha: String) {
        KoiAccountingNetApi.standard.loginCaptchaSubmit(phone: phone, captcha: captcha) { result in
            switch result {
            case .success(let user):
                // 保存登录信息
                KoiAccountingUserManager.standard.login(user: user)
                self.loginState = "success"
                self.isLoading = false
            case .failure(let error):
                self.loginState = "fail"
                self.isLoading = false
                logger.error("sendCaptcha fail \(error)")
            }
        }
    }
    
    func sendCaptcha(phone: String) {
        isLoading = true
        KoiAccountingNetApi.standard.sendCaptcha(phone: phone) { result in
            switch result {
            case .success:
                self.captchaState = "success"
                self.isLoading = true
            case .failure(let error):
                self.captchaState = "fail"
                self.isLoading = false
                logger.error("sendCaptcha fail \(error)")
            }
        }
    }
}
